package com.yami.shop.multishop.controller;

import com.yami.shop.bean.model.ShopLink;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.ShopLinkService;
import com.yami.shop.service.SinaShortUrlService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 新浪短链接控制器
 */
@RestController
@RequestMapping("/shortUrl")
@Api(tags = "新浪短链接接口", description = "提供新浪短链接生成相关接口")
public class SinaShortUrlController {

    @Resource
    private SinaShortUrlService sinaShortUrlService;
    @Autowired
    private ShopLinkService shopLinkService;

    /**
     * 生成短链接
     */
    @PostMapping("/generate")
    @ApiOperation(value = "生成短链接并设置", notes = "将长链接转换为短链接")
    public ResponseEntity<String> generateShortUrl(
            @ApiParam(value = "长链接", required = true)
            @RequestParam String longUrl) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        ResponseEntity<String> shortUrl = sinaShortUrlService.generateShortUrl(longUrl);
        ShopLink shopLink = new ShopLink();
        shopLink.setShopId(shopId);
        shopLink.setShortUrl(shortUrl.getBody());
        shopLinkService.updateShopLink(shopLink);
        return ResponseEntity.ok().body(shortUrl.getBody());
    }

    /**
     * 批量生成短链接
     */
    @PostMapping("/batchGenerate")
    @ApiOperation(value = "批量生成短链接", notes = "批量将长链接转换为短链接，最多支持20个链接")
    public ResponseEntity<String[]> batchGenerateShortUrl(
            @ApiParam(value = "长链接数组", required = true)
            @RequestBody String[] longUrls) {
        return sinaShortUrlService.batchGenerateShortUrl(longUrls);
    }
} 